package com.iwdnb.gkgz.common.utils;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import com.iwdnb.bmnf.common.model.result.Result;
import com.iwdnb.bmnf.common.service.CacheService;
import com.iwdnb.bmnf.common.utils.HttpClientUtils;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.CacheConstants.BLACK_STOCK_KEY;

/**
 * 股票黑名单列表,接口可在https://stockapi.com.cn查看
 * 上交所主板股票代码是600、601、603、605开头，深交所主板股票代码000开头，中小板002开头，创业板是300开头。科创板股票代码的开头数字是688
 */
@Slf4j
@Component
public class StockBlackListUtils implements ApplicationContextAware {

    private static CacheService cacheService;
    /**
     * st股票列表
     */
    private static final String ST_URL = "https://stockapi.com.cn/v1/base/st";

    /**
     * 能买的股票列表
     * 剔除黑名单股票(st、证券),创业板,科创板
     *
     * @param codes
     * @return
     */
    public static List<String> getCanBuyCodeList(List<String> codes) {
        List<String> list = new ArrayList<>();
        String blackList = cacheService.get(BLACK_STOCK_KEY);
        if (StringUtils.isBlank(blackList)) {
            blackList = "";
        }
        for (String code : codes) {
            String simpleCode = code.replaceAll("sh", "").replaceAll("sz", "");
            //创业板和科创板不买
            if (simpleCode.startsWith("300") || simpleCode.startsWith("301") || simpleCode.startsWith("688")) {
                continue;
            }
            if (blackList.contains(code)) {
                continue;
            }
            list.add(code);
        }
        return list;
    }

    public static boolean canBuy(String code) {
        String simpleCode = code.replaceAll("sh", "").replaceAll("sz", "");
        //创业板和科创板不买
        if (simpleCode.startsWith("300") || simpleCode.startsWith("688")) {
            return false;
        }
        //黑名单股票不买
        if (isBlack(code)) {
            return false;
        }
        return true;
    }

    public static boolean isBlack(String code) {
        String blackList = cacheService.get(BLACK_STOCK_KEY);
        if (StringUtils.isBlank(blackList)) {
            return false;
        }
        return blackList.contains(code);
    }

    public static void initBlackStockList() {
        String body = HttpClientUtils.get(ST_URL);
        Result<List<StockDayData>> result = JSON.parseObject(body, new TypeReference<Result<List<StockDayData>>>() {});
        if (!result.getCode().equals("20000")) {
            log.error("stUrl request error:" + body);
            return;
        }
        String blackList = cacheService.get(BLACK_STOCK_KEY);
        if (StringUtils.isBlank(blackList)) {
            blackList = "";
        }
        List<StockDayData> stockDayDataList = result.getData();
        for (StockDayData stockDayData : stockDayDataList) {
            String[] array = stockDayData.getCode().toLowerCase().split("\\.");
            String code = array[1] + array[0];
            if (!blackList.contains(code)) {
                blackList += code + ",";
            }
        }
        cacheService.put(BLACK_STOCK_KEY, blackList);
    }

    public static void addBlackStock(String codes) {
        String blackList = cacheService.get(BLACK_STOCK_KEY);
        if (StringUtils.isBlank(blackList)) {
            blackList = "";
        }
        String[] codeArray = codes.split(",");
        for (String code : codeArray) {
            if (!blackList.contains(code)) {
                blackList += code + ",";
            }
        }
        cacheService.put(BLACK_STOCK_KEY, blackList);
    }

    public static String getBlackStockList() {
        return cacheService.get(BLACK_STOCK_KEY);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        cacheService = applicationContext.getBean(CacheService.class);
    }
}
